#include "stdafx.h"
#include "admindialog.h"
#include "popupmenu.h"

AdminDialog::AdminDialog( QWidget * parent )
:   DialogBase( parent ),
    m_topicManager( this )
{
    if (! m_topicManager.dataTableManager().select( "", * ui.topicTableView ))
    {
        QMetaObject::invokeMethod( this, "close", Qt::QueuedConnection );
        return;
    }

    ui.topicTableView->init();
    ui.topicTableView->setModel( & m_topicManager.dataTableManager().sortModel() );
    ui.topicTableView->restoreHeaderViewState( "TopicDialogHeader" );

    connect( ui.topicTableView->selectionModel(), SIGNAL( selectionChanged ( const QItemSelection &, const QItemSelection & ) ), this, SLOT( onSelectionChanged ( const QItemSelection &, const QItemSelection & ) ) );
    connect( ui.addPushButton, SIGNAL( clicked() ), this, SLOT( onAdd() ) );
    connect( ui.modifyPushButton, SIGNAL( clicked() ), this, SLOT( onUpdate() ) );
    connect( ui.deletePushButton, SIGNAL( clicked() ), this, SLOT( onRemove() ) );
    connect( ui.okPushButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
    connect( ui.topicTableView, SIGNAL( contextMenu( const QPoint &, const QModelIndex & ) ), this, SLOT( onTopicViewContextMenu( const QPoint &, const QModelIndex & ) ) );
    connect( ui.topicTableView, SIGNAL( doubleClicked ( const QModelIndex & ) ), SLOT( onTopicTableViewDoubleClicked( const QModelIndex & ) ) );
}

AdminDialog::~AdminDialog()
{
    ui.topicTableView->saveHeaderViewState( "TopicDialogHeader" );
}

void AdminDialog::onAdd()
{
    m_topicManager.executeAction( ManagerBase::ActionBase::New );
}

void AdminDialog::onUpdate()
{
    int standardModelRow;
    const QStandardItem * item;
    if (m_topicManager.dataTableManager().getStandardModelRowAndStandardItemFromProxyIndex( ui.topicTableView->selectionModel(), standardModelRow, item ))
    {
        m_topicManager.executeAction( ManagerBase::ActionBase::Update, standardModelRow, item );
    }
}

void AdminDialog::onRemove()
{
    int standardModelRow;
    const QStandardItem * item;
    if (m_topicManager.dataTableManager().getStandardModelRowAndStandardItemFromProxyIndex( ui.topicTableView->selectionModel(), standardModelRow, item ))
    {
        m_topicManager.executeAction( ManagerBase::ActionBase::Delete, standardModelRow, item );
    }
}

void AdminDialog::onTopicViewContextMenu( const QPoint & pos, const QModelIndex & selModelIndex )
{
    PopupMenu popupMenu( this );

    int standardModelRow;
    const QStandardItem * item;
    m_topicManager.dataTableManager().getStandardModelRowAndStandardItemFromProxyIndex( selModelIndex, standardModelRow, item );
    ManagerBase::ActionStructVector actionStructVector = m_topicManager.createActionList( item );
    ManagerBase::ActionStructVector::const_iterator it;
    FOREACH (it, actionStructVector)
    {
        popupMenu.addItem( it->actionName, it->actionId, it->image );
    }

    QVariant varData = popupMenu.execAndSelectData( pos );
    if (! varData.isValid())
        return;

    ManagerBase::ActionBase::Enum actionBase = (ManagerBase::ActionBase::Enum)varData.toInt();
    m_topicManager.executeAction( actionBase, standardModelRow, item );
}

void AdminDialog::onTopicTableViewDoubleClicked( const QModelIndex & modelIndex )
{
   int standardModelRow;
   const QStandardItem * item;
   if (m_topicManager.dataTableManager().getStandardModelRowAndStandardItemFromProxyIndex( modelIndex, standardModelRow, item ))
   {
       m_topicManager.executeAction( ManagerBase::ActionBase::Update, standardModelRow, item );
   }
}

void AdminDialog::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
{
    QModelIndexList indexes = selected.indexes();
    bool selectedIndex = ! indexes.isEmpty();

    ui.modifyPushButton->setEnabled( selectedIndex );
    ui.deletePushButton->setEnabled( selectedIndex );
}